package com.web.kdl.controller.Minio;

import com.web.kdl.common.result.CommonResult;
import com.web.kdl.common.result.MessageConstants;
import com.web.kdl.config.Minio.MinioProperties;
import com.web.kdl.pojo.Vo.SvgVo;
import com.web.kdl.pojo.dto.TableDto;
import com.web.kdl.pojo.form.RT1000Form;
import com.web.kdl.service.Minio.MinioService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author： shulibin
 * @Date： 2025/9/3 11:33
 * @Describe：
 */
@RestController
@Slf4j
public class MinioController {
    @Autowired
    private MinioService minioService;


    @PostMapping("/createSite")
    public CommonResult createSite(@RequestBody TableDto form) throws IOException {
        return minioService.createSite(form.getSiteName());
    }
    @PostMapping("/createFlyWheel")
    public CommonResult createFlyWheel(@RequestBody TableDto form) throws IOException {
        return minioService.createFlyWheelFile(form);
    }

    @PostMapping("/getUrl") // TODO 由于未使用该接口 暂未修改
    public CommonResult getUrl(@RequestBody TableDto dto) {
        String fileUrl = minioService.getFileUrl(dto);
        return CommonResult.success(fileUrl);
    }

    // 下载自定义控件数据存储文件
    @PostMapping("/downloadFile")
    public CommonResult downloadFile(@RequestBody TableDto dto) throws Exception {
        byte[] bytes = minioService.downloadFile(dto);
        return CommonResult.success(bytes);
    }


    @DeleteMapping("/deleteFile")
    public CommonResult deleteFile(@RequestBody TableDto dto) {
        String fileUrl = minioService.getFileUrl(dto);
        minioService.deleteFile(fileUrl);
        return CommonResult.success();
    }
    @PostMapping(value = "uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult uploadFile(@RequestPart("dto") TableDto dto, @RequestPart("file") MultipartFile file) {
        String url = minioService.uploadFile(dto, file);
        return CommonResult.success(url);
    }

    @PostMapping ("/uploadSvg")
    public CommonResult uploadSvg(MultipartFile file,String type,String name) {
        return minioService.uploadSvgFile(file,type,name);
    }

    @GetMapping ("/getAllSvgUrl")
    public CommonResult getAllSvgUrl() throws Exception {
        List<SvgVo> allSvgUrl = minioService.getAllSvgUrl();
        return CommonResult.success(allSvgUrl);
    }
    // 上传背景图片
    @PostMapping("/uploadImage")
    public CommonResult uploadImage(@RequestPart("file") MultipartFile file) {
        return minioService.uploadImage(file);
    }
    // 获取背景图片
    @GetMapping("/getImageUrl")
    public CommonResult getImageUrl() {
        String imageUrl = minioService.getImageUrl();
        return CommonResult.success(imageUrl);
    }
}
